package com.qcby.byc.demo0422.controller;


import com.qcby.byc.demo0422.entity.Demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @className: DemoController
 * @description: 测试
 * @author: lxt
 * @create: 2021-03-20 10:14
 **/
@Controller
@RequestMapping  // 默认 /
public class DemoController {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private HttpSession httpSession;

    @RequestMapping(path= "demo")  // demo ：   ip+端口 +项目根路径 +  / + demo
    @ResponseBody // 返回类型是一个对象：可以是字符串、对象、List等等；不是一个页面
    public String demo(Model model
                       // 走默认装配规则，参数名称和名字一致，选传
        , String name
                       // 参数名称和名字默认一直，必传参数，不传报错
            , @RequestParam String name0
                       // 通过value属性定义名称，必传参数，不传报错
            , @RequestParam(value = "name1") String name1
                       // 通过name属性定义，非必传参数
            , @RequestParam(name = "name2",required = false) String name2
                       // 通过value属性定义，必传参数，有默认值，可以不传自动赋值
            , @RequestParam(value = "name3",defaultValue = "t3") String name3
                       // 自定义参数名字，不必加name或者value属性，必传
            , @RequestParam("name444") String name4
    ){

        // 0不能作为被除数
//        int c= 1 / 0;

//        for (int i = 0; i < 100; i++) {
//            System.out.println("name:"+name);
//            System.out.println("name:"+name);
//            System.out.println("i:"+i);
//            if(i == 56){
//                int c = i/0;
//            }
//        }
        System.out.println("name:"+name);
        System.out.println("name0:"+name0);
        System.out.println("name1:"+name1);
        System.out.println("name2:"+name2);
        System.out.println("name3:"+name3);
        System.out.println("name4:"+name4);
        return "hello";
    }

    @RequestMapping(value={
            "demo1/{name1}/{name2}/{name3}"
            // 配合name3选传，此种场景应用较少，一般使用此注解均为必传参数
            ,"demo1/{name1}/{name2}"
    })
    @ResponseBody
    public String demo1(Model model
                        //注意，没加注解，使用问号后追加参数请求
            , String name
                        // 必传
            , @PathVariable String name1
                        // 必传
            , @PathVariable("name2") String name2
                        // 选传
            , @PathVariable(value = "name3",required = false) String name3
    ){
        System.out.println("name:"+name);
        System.out.println("name1:"+name1);
        System.out.println("name2:"+name2);
        System.out.println("name3:"+name3);
        return "hello";
    }





//    @RequestMapping("demoBefore")

//    @GetMapping("demoBefore")
    @RequestMapping(
            method = {RequestMethod.GET,RequestMethod.POST}
            ,value = "demoBefore"
    )
    public String demoBefore(){
//        if(httpSession.getAttribute("user") == null){
//            return "login";
//        }
        return "demo";
    }



    // form表单传参

    /**
     *      添加、修改  都是以对象作为参数;
     *      查询时以对象为参数，查询条件变动时，同理
     * @param demo
     * @return
     */
    @ResponseBody  //返回是一个对象 不是渲染页面
    @RequestMapping("demo2")
    public Demo demo2(Demo demo){
        System.out.println("demo:"+demo);
        return demo;
    }


    /**
     * @RequestBody  要求前台通过json格式传递参数  header:   content-type : application/josn
     * @param demo
     * @return
     */
    @ResponseBody
    @PostMapping("demo22") // 只允许使用post方式
//    @GetMapping
//    @RequestMapping(
//            method = {RequestMethod.POST},value = "demo22"
//    )
    public Demo demo22(@RequestBody Demo demo){
        System.out.println("demo:"+demo);
        return demo;
    }


    @ResponseBody
    @RequestMapping("demo3")
    public Long[] demo3(Long[] ids){
        System.out.println("ids:"+ids.length);
        return ids;
    }

    @ResponseBody
    @RequestMapping("demo33")
    public Long[] demo33(@RequestBody Long[] ids){
        System.out.println("ids:"+ids.length);
        return ids;
    }



    @ResponseBody
    @RequestMapping("demo4")
    public List<Long> demo4(@RequestParam List<Long> idList){
        // 0不能作为被除数
//        int c= 1/ 0;
        System.out.println("ids:"+idList);
        return idList;
    }

    @ResponseBody
    @RequestMapping("demo5")
    public List<Long> demo5(@RequestBody List<Long> idList){
        System.out.println("ids:"+idList);
        return idList;
    }


    @ResponseBody
    @RequestMapping("demo6")
    public Demo demo6(Demo demo){
        System.out.println("demo:"+demo);
        return demo;
    }



    @ResponseBody
    @RequestMapping("demo7")
    public Demo demo7(@RequestBody Demo demo){
        System.out.println("demo:"+demo);
        return demo;
    }


}
